package br.odb.portalizer.tests;

import junit.framework.Assert;

import org.junit.Test;

import br.odb.libscene.Sector;
import br.odb.libscene.World;
import br.odb.portalizer.WorldPartitioner;

public class TestWorldPartitioner {

	@Test
	public final void test() {
		World  world = new World();
		World  worldOpt;
		Sector sector;
		
		sector = new Sector( 0.0f, 255.0f, 0.0f, 255.0f, 0.0f, 255.0f );
		world.addSector( sector );
		sector = new Sector( 0.0f, 2.0f, 0.0f, 9.0f, 0.0f, 9.0f );
		world.addSector( sector );
		sector = new Sector( 2.0f, 5.0f, 0.0f, 9.0f, 4.0f, 5.0f );
		world.addSector( sector );

		worldOpt = WorldPartitioner.optimize( world );
		Assert.assertEquals( 5, worldOpt.getTotalSectors() );
	}

	@Test
	public final void testIdPreservationOfSolids() {
		World  world = new World();
		World  worldOpt;
		Sector sector;
		


		sector = new Sector( 0.0f, 255.0f, 0.0f, 255.0f, 0.0f, 255.0f );
		world.addSector( sector );
		sector = new Sector( 0.0f, 9.0f, 0.0f, 9.0f, 0.0f, 9.0f );
		world.addSector( sector );
		sector = new Sector( 9.0f, 18.0f, 0.0f, 9.0f, 0.0f, 9.0f );
		world.addSector( sector );
		sector = new Sector( 0.0f, 9.0f, 0.0f, 9.0f, 9.0f, 18.0f );
		world.addSector( sector );
		sector = new Sector( 9.0f, 18.0f, 0.0f, 9.0f, 9.0f, 18.0f );
		world.addSector( sector );

		sector = new Sector( 8.0f, 10.0f, 4.0f, 5.0f, 8.0f, 10.0f );
		world.addSector( sector );
		sector.setId( -1 );

		worldOpt = WorldPartitioner.optimize( world, true );
		WorldPartitioner.renewIds( worldOpt, true );
		WorldPartitioner.guessLinks( worldOpt, false );
		WorldPartitioner.removeNegatives( worldOpt );

		int count = 0;
		
		for ( int c = 0; c < worldOpt.getTotalSectors(); ++c ) {
			
			if ( worldOpt.getSector( c ).getId() == -1 )
				++count;
		}
		
		Assert.assertEquals( 0, count );
		Assert.assertEquals( 45, worldOpt.getTotalSectors() );
	}
	
	@Test
	public final void testIdPreservation() {
		World  world = new World();
		World  worldOpt;
		Sector sector;
		


		sector = new Sector( 0.0f, 255.0f, 0.0f, 255.0f, 0.0f, 255.0f );
		world.addSector( sector );
		sector = new Sector( 0.0f, 9.0f, 0.0f, 9.0f, 0.0f, 9.0f );
		world.addSector( sector );

		sector = new Sector( 4.0f, 5.0f, 0.0f, 9.0f, 4.0f, 5.0f );
		world.addSector( sector );
		//sector.setSolid( true );
		sector.setId( -1 );

		
		worldOpt = WorldPartitioner.optimize( world, true );
		WorldPartitioner.renewIds( worldOpt, true );
		WorldPartitioner.guessLinks( worldOpt, false );
		WorldPartitioner.removeNegatives( worldOpt );
		int count = 0;
		
		for ( int c = 0; c < worldOpt.getTotalSectors(); ++c ) {
			
			if ( worldOpt.getSector( c ).getId() == -1 )
				++count;
			
			Assert.assertFalse( worldOpt.getSector( c ).isSolid() );
			Assert.assertFalse( worldOpt.getSector( c ).isDegenerate() );
		}
		
		Assert.assertEquals( 0, count );
		worldOpt.saveToDiskAsLevel( "/Users/danielmonteiro/tmp.level" );
	}

}
